package common.io;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Arrays;

/**
 * Created by Gerardo on 1/24/14.
 */
public class Printer extends SimpleFileVisitor<Path> implements FileVisitor<Path> {
	private int deep = 0;

	@Override
	public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
		System.out.printf("%s-%s %n", getDeep(), file.getFileName());
		return FileVisitResult.CONTINUE;
	}

	@Override
	public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs) throws IOException {
		System.out.printf("%s*%s %n", getDeep(), dir.getFileName());
		++deep;
		return FileVisitResult.CONTINUE;
	}

	@Override
	public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException {
		--deep;
		return FileVisitResult.CONTINUE;
	}

	private String getDeep() {
		char[] spaces = new char[deep];
		Arrays.fill(spaces, ' ');
		return new String(spaces);
	}
}
